package com.example.onlinemusicserver2024.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusicserver2024.common.R;
import com.example.onlinemusicserver2024.mapper.AdminMapper;
import com.example.onlinemusicserver2024.model.domain.Admin;
import com.example.onlinemusicserver2024.model.request.AdminRequest;
import com.example.onlinemusicserver2024.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * 接口实现类
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public R verityPasswd(AdminRequest adminRequest, HttpSession session) {
        // todo 完成具体登录的逻辑处理
        // 获取参数中的用户名和密码
        String username = adminRequest.getUsername();
        String pwd = adminRequest.getPassword();
        // 设置查询条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username);
        queryWrapper.eq("password", pwd);
        // 根据查询条件，获取查询记录数
        Long count = adminMapper.selectCount(queryWrapper);
        if (count > 0) {
            // 验证成功，可以登录
            session.setAttribute("name", adminRequest.getUsername());
            return R.success("登录成功");
        } else {
            // 验证失败
            return R.error("用户名或密码错误");
        }
    }
}
